var pageParams;

function addLoad() {
    $("#b_timerId").val(pageParams.pId);
}

//返回值为需不需要关闭窗口
function yesOperator() {
    var retValue = addData();
    return retValue;
}

function updateLoad() {
    var params = $$.getQueryString();
    $$.ajax({
        url: "/rest/BaseTimerDataBusiness/query",
        data: {
            id: params.rowId
        },
        success: function (data) {
            $("#b_id").val(data.id);
            $("#b_timerId").val(data.timerId);
            $("#b_appCode").val(data.appCode);
            $("#b_itemKey").val(data.itemKey);
            $("#b_itemKeyOld").val(data.itemKey);
            $("#b_itemName").val(data.itemName);
            $("#b_itemValue").val(data.itemValue);
            $("#b_status").val(data.status);
            $("#b_rowSort").val(data.rowSort);
            $("#b_remarks").val(data.remarks);

            var status = data.status;
            $("input[name^='b_status_']").each(function () {
                if ($(this).val() == status) {
                    this.checked = 'checked';
                }
            });

            $("#b_timer_dataCodeOld").val(data.timer_dataCode);
        }
    });
}

function validForm() {
    return $('#theform').valid();
}

function addData() {
    var param = {};
    var validFormRet;
    $$.form({
        async: false,
        able: function () {
            validFormRet = false;
        },
        checkForm: validForm,
        success: function () {
            validFormRet = true;
        }
    });
    return validFormRet;
}

function forwardCalcValid() {
    $("#theform").validate({
        rules: {


            b_itemKey: {

                required: true,

                minlength: 1,
                number: false,
                isCode:true

            },
            b_itemName: {
                required: true,

                minlength: 1,
                number: false
            },
            b_itemValue: {

                required: true,

                minlength: 1,
                number: false
            },
            b_status: {

                required: true,

                minlength: 1,
                number: false
            },
            b_rowSort: {

                required: true,

                minlength: 1,
                number: false
            },

            b_rowSort: {
                required: true,
                minlength: 3,
                isIntGteZero: false,
            }
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errortips",
        success: 'valid',
        messages: {
            b_timerId: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_appCode: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_itemKey: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_itemName: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_itemValue: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_rowSort: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            }
        },
    });
}

$(function () {
    pageParams = $$.getQueryString();
    forwardCalcValid();
    $("input[name^='b_status']").click(function () {
        $("#b_status").val($(this).val());
    });
});

jQuery.validator.addMethod("isCode", function (value, element) {
    //校验编码是否存在
    var itemKeyOld = $("#b_itemKeyOld").val();
    var itemKey = $("#b_itemKey").val();
    if (itemKey == itemKeyOld) {
        return true;
    } else {
        var args = {};
        args.c_timerId_1 = pageParams.pId;
        args.c_itemKey_1 = $("#b_itemKey").val();
        args.page = 0;
        args.pageSize = 5;
        args.isPage = 1;
        var exists;
        $$.ajax({
            url: "/bootstrap/basegrid/BaseTimerDataBusiness/list",
            async: false,
            data: args,
            success: function (data) {
                if (data.length && data.length > 0) {
                    exists = true;
                } else {
                    exists = false;
                }
            }
        });
        if (exists) {
            $(element).data('error-msg', '键不能重复！');
            return false;
        }
        return true;
    }
}, function (params, element) {
    return $(element).data('error-msg');
});
/**/
